﻿using System;
using System.Numerics;
using RpcHelper;
using SqlExecHelper;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Collect
{
        internal class AutoTaskItemCollect : BasicCollect<AutoTaskItemDAL>, IAutoTaskItemCollect
        {
                public Guid Add (AutoTaskItem item)
                {
                        AutoTaskDAL task = new AutoTaskDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                Guid id= base.BasicDAL.Add (item);
                                task.ResetVerNum (item.TaskId);
                                tran.Commit ();
                                return id;
                        }
                }

                public TaskItem[] Gets (Guid taskId)
                {
                        return base.BasicDAL.Gets (taskId);
                }
                public void SetIsEnable (AutoTaskItem item, bool isEnable)
                {
                        if (item.IsEnable == isEnable)
                        {
                                return;
                        }
                        AutoTaskDAL task = new AutoTaskDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                base.BasicDAL.SetIsEnable (item.Id, isEnable);
                                task.ResetVerNum (item.TaskId);
                                tran.Commit ();
                        }
                }
                public AutoTaskItem Get (Guid id)
                {
                        AutoTaskItem item = this.BasicDAL.Get (id);
                        if (item == null)
                        {
                                throw new ErrorException ("rpc.task.item.not.find");
                        }
                        return item;
                }

                public void Set (AutoTaskItem item, SetTaskItem set)
                {
                        if (item.IsEquals (set))
                        {
                                return;
                        }
                        AutoTaskDAL task = new AutoTaskDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                base.BasicDAL.Set (item.Id, set);
                                task.ResetVerNum (item.TaskId);
                                tran.Commit ();
                        }
                }
                public void Delete (AutoTaskItem item)
                {
                        AutoTaskDAL task = new AutoTaskDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                base.BasicDAL.Delete (item.Id);
                                task.ResetVerNum (item.TaskId);
                                tran.Commit ();
                        }
                }

                public TaskItemTitle[] GetTitles (Guid[] ids)
                {
                        return base.BasicDAL.GetTitles (ids);
                }
        }
}
